package com.zscat.mallplus.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.sys.entity.SysLogininfor;
import com.zscat.mallplus.sys.mapper.SysLogininforMapper;
import com.zscat.mallplus.sys.service.ISysLogininforService;
import com.zscat.mallplus.sys.util.Constants;
import com.zscat.mallplus.sys.util.SpringUtils;
import com.zscat.mallplus.util.AddressUtils;
import com.zscat.mallplus.util.IpUtils;
import com.zscat.mallplus.util.ServletUtils;
import com.zscat.mallplus.util.UserUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.TimerTask;

@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor> implements ISysLogininforService {

    @Override
    public void saveSysLogininfor(HttpServletRequest request, int status, String message, Date loginTime) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(request);
        String address = AddressUtils.getRealAddressByIP(ip);
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        // 封装对象
        SysLogininfor logininfor = new SysLogininfor();
        logininfor.setLoginName(UserUtils.getCurrentMember().getNickName());
        logininfor.setIpaddr(ip);
        logininfor.setLoginLocation(address);
        logininfor.setBrowser(browser);
        logininfor.setOs(os);
        logininfor.setMsg(message);
        logininfor.setStatus(String.valueOf(status));
        logininfor.setLoginTime(new Date());
        logininfor.setStoreId(UserUtils.getCurrentMember().getStoreId());
        this.save(logininfor);
    }

    /***

     * 获取客户端IP地址;这里通过了Nginx获取;X-Real-IP,

     * @param request

     * @return

     */

    public static String getClientIP(HttpServletRequest request) {

        //String fromSource = "X-Real-IP";
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {

            ip = request.getHeader("X-Forwarded-For");
            //fromSource = "X-Forwarded-For";
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            //fromSource = "Proxy-Client-IP";
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
            //fromSource = "WL-Proxy-Client-IP";
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            //fromSource = "request.getRemoteAddr";
        }
        return ip;

    }
}